package com.baikeyang.service.activity.impl;

import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.bean.activity.type.ActivityType;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Dictionaries;
import com.baikeyang.common.model.User;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.common.util.shiro.SecurityUtil;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.service.activity.IActivityTypeService;
import com.baikeyang.service.dictionaries.IDictionariesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service(value = "activityTypeServiceImpl")
public class ActivityTypeServiceImpl implements IActivityTypeService {

	private static final String TYPE_CODE = "activity_type";

	@Autowired
	IDictionariesService dictionariesService;

	@Override
	public List<Dictionaries> getTypeAll() {
		return dictionariesService.queryDictionariesByCode(TYPE_CODE);
	}

	@Override
	public IPage<Dictionaries> getTypeAllPage(Page<Dictionaries> pageParam) {
		IPage<Dictionaries> iPage = dictionariesService.queryDictionariesPageByCode(pageParam, TYPE_CODE);
		return iPage;
	}

	@Override
	public ResponseResult saveActivityType(ActivityType type) {
		Dictionaries parentDictionaries = dictionariesService.queryDictionaryByCode(TYPE_CODE);
		if(null != parentDictionaries){
			Dictionaries dictionaries = new Dictionaries();
			dictionaries.setValue(dictionariesService.queryMaxValueByCode(TYPE_CODE) + 1);
			dictionaries.setSort(type.getSort());
			dictionaries.setName(type.getName());
			dictionaries.setRemark(type.getRemark());
			dictionaries.setState(type.getStatus());
			dictionaries.setParent(parentDictionaries.getId());
			return dictionariesService.saveDictionary(dictionaries);
		}
		return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
	}

	@Override
	public ResponseResult deleteActivityType(int id) {
		// TODO 删除之前需要判断该类型是否已被使用
		int result = dictionariesService.deleteDictionary(id);
		if(result > 0){
			return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
		}
		return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
	}

	@Override
	public Dictionaries getActivityType(int id) {
		return dictionariesService.queryDictionaryById(id);
	}

	@Override
	public ResponseResult updateActivityType(ActivityType type) {
		Dictionaries dictionaries = getActivityType(type.getId());
		if(null != dictionaries){
			dictionaries.setSort(type.getSort());
			dictionaries.setName(type.getName());
			dictionaries.setRemark(type.getRemark());
			dictionaries.setState(type.getStatus());
			return dictionariesService.updateDictionary(dictionaries);
		}
		return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
	}
}
